﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Microsoft.Http;

namespace D3DSS.Http
{
    /// <summary>
    /// Http请求类
    /// </summary>
    public class HttpRequest
    {

        /// <summary>
        /// Get方法
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <returns></returns>
        public static Tuple<T, HttpStatusCode> Get<T>(string url)
        {
            using (var httpClient = new HttpClient())
            {
                var response = httpClient.Get(url);
                string jsonStr = response.Content.ReadAsString();
                return new Tuple<T, HttpStatusCode>(JsonSerialize.DeSerialize<T>(jsonStr), response.StatusCode);
            }
        }

        /// <summary>
        /// Get方法
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <returns></returns>
        public static Tuple<T, HttpStatusCode> Get<T>(string url, object authorization)
        {
            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultHeaders[HttpRequestHeader.Authorization.ToString()] = JsonSerialize.Serialize(authorization);
                var response = httpClient.Get(url);
                string jsonStr = response.Content.ReadAsString();
                return new Tuple<T, HttpStatusCode>(JsonSerialize.DeSerialize<T>(jsonStr), response.StatusCode);
            }
        }



        /// <summary>
        /// Get方法
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="queryStrings">参数键值对</param>
        /// <returns></returns>
        public static Tuple<T, HttpStatusCode> Get<T>(string url, KeyValuePair<string, string> queryStrings)
        {
            using (var httpClient = new HttpClient())
            {
                var response = httpClient.Get(new Uri(url), new List<KeyValuePair<string, string>>() { queryStrings });
                string jsonStr = response.Content.ReadAsString();
                return new Tuple<T, HttpStatusCode>(JsonSerialize.DeSerialize<T>(jsonStr), response.StatusCode);
            }
        }

        /// <summary>
        /// Get方法
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="queryStrings">参数键值对</param>
        /// <returns></returns>
        public static Tuple<T, HttpStatusCode> Get<T>(string url, IEnumerable<KeyValuePair<string, string>> queryStrings)
        {
            using (var httpClient = new HttpClient())
            {
                var response = httpClient.Get(new Uri(url), queryStrings);
                string jsonStr = response.Content.ReadAsString();
                return new Tuple<T, HttpStatusCode>(JsonSerialize.DeSerialize<T>(jsonStr), response.StatusCode);
            }
        }

        /// <summary>
        /// Post方法
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="obj">实体</param>
        /// <returns></returns>
        public static Tuple<T, HttpStatusCode> Post<T>(string url, object obj)
        {
            using (var httpClient = new HttpClient())
            {
                var response = httpClient.Post(url, HttpContent.Create(JsonSerialize.Serialize(obj, Encoding.UTF8), "application/json"));
                string jsonStr = response.Content.ReadAsString();
                return new Tuple<T, HttpStatusCode>(JsonSerialize.DeSerialize<T>(jsonStr), response.StatusCode);
            }
        }

        /// <summary>
        /// Post方法
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="obj">实体</param>
        /// <returns></returns>
        public static HttpStatusCode Post(string url, object obj)
        {
            using (var httpClient = new HttpClient())
            {
                return httpClient.Post(url, HttpContent.Create(JsonSerialize.Serialize(obj, Encoding.UTF8), "application/json")).StatusCode;
            }
        }

        /// <summary>
        /// Post方法
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="obj">实体</param>
        /// <returns></returns>
        public static Tuple<T, HttpStatusCode> Post<T>(string url, object obj, object authorization)
        {
            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultHeaders[HttpRequestHeader.Authorization.ToString()] = JsonSerialize.Serialize(authorization);
                var response = httpClient.Post(url, HttpContent.Create(JsonSerialize.Serialize(obj, Encoding.UTF8), "application/json"));
                string jsonStr = response.Content.ReadAsString();
                return new Tuple<T, HttpStatusCode>(JsonSerialize.DeSerialize<T>(jsonStr), response.StatusCode);
            }
        }
    }
}
